#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton_0, SIGNAL(clicked()), this, SLOT(on_button0_clicked()));
    connect(ui->pushButton_1, SIGNAL(clicked()), this, SLOT(on_button1_clicked()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(on_button2_clicked()));
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(on_button3_clicked()));
    connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(on_button4_clicked()));
    connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(on_button5_clicked()));
    connect(ui->pushButton_6, SIGNAL(clicked()), this, SLOT(on_button6_clicked()));
    connect(ui->pushButton_7, SIGNAL(clicked()), this, SLOT(on_button7_clicked()));
    connect(ui->pushButton_8, SIGNAL(clicked()), this, SLOT(on_button8_clicked()));
    connect(ui->pushButton_9, SIGNAL(clicked()), this, SLOT(on_button9_clicked()));
    connect(ui->pushButton_jia, SIGNAL(clicked()), this, SLOT(on_buttonJia_clicked()));
    connect(ui->pushButton_jian, SIGNAL(clicked()), this, SLOT(on_buttonJian_clicked()));
    connect(ui->pushButton_xing, SIGNAL(clicked()), this, SLOT(on_buttonChen_clicked()));
    connect(ui->pushButton_chu, SIGNAL(clicked()), this, SLOT(on_buttonChu_clicked()));
    connect(ui->pushButton_ce, SIGNAL(clicked()), this, SLOT(on_buttonClear_clicked()));
    connect(ui->pushButton_deng, SIGNAL(clicked()), this, SLOT(on_buttonDeng_clicked()));
    connect(ui->pushButton_bs, SIGNAL(clicked()), this, SLOT(on_buttonBs_clicked()));
    connect(ui->pushButton_point, SIGNAL(clicked()), this, SLOT(on_buttonPoint_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_buttonBs_clicked()
{
    op2 = ui->textEdit->toPlainText().toInt();
    op2 /= 10;
    QString strNumber;
    strNumber.setNum(op2);
    if(op2 == 0)
    {
        ui->textEdit->setText("");
    }else
    {
        ui->textEdit->setText(strNumber);
    }
}

void MainWindow::on_buttonDeng_clicked()
{
    op2 = ui->textEdit->toPlainText().toInt();
    switch (op)
    {
    case OPERATOR_ADD:
        op1 = op1 + op2;
        break;
    case OPERATOR_MINUS:
        op1 = op1 - op2;
        break;
    case OPERATOR_MULTIPLY:
        op1 = op1 * op2;
        break;
    case OPERATOR_DIVIDE:
        if (op2 != 0)
        {
            op1 = op1 / op2;
        }
        break;
    case OPERATOR_UP:
        op1 = pow(op1, op2);
    }
    QString strNumber;
    strNumber.setNum(op1);
    ui->textEdit->setText(strNumber);
}

void MainWindow::on_buttonClear_clicked()
{
    ui->textEdit->setText("");
}

void MainWindow::on_buttonJia_clicked()
{
    op1 = ui->textEdit->toPlainText().toInt();
    op = OPERATOR_ADD;
    ui->textEdit->setText("");
}

void MainWindow::on_buttonPoint_clicked()
{
    op1 = ui->textEdit->toPlainText().toInt();
    op = OPERATOR_UP;
    ui->textEdit->setText("");
}

void MainWindow::on_buttonJian_clicked()
{
    op1 = ui->textEdit->toPlainText().toInt();
    op = OPERATOR_MINUS;
    ui->textEdit->setText("");
}

void MainWindow::on_buttonChen_clicked()
{
    op1 = ui->textEdit->toPlainText().toInt();
    op = OPERATOR_MULTIPLY;
    ui->textEdit->setText("");
}

void MainWindow::on_buttonChu_clicked()
{
    op1 = ui->textEdit->toPlainText().toInt();
    op = OPERATOR_DIVIDE;
    ui->textEdit->setText("");
}

void MainWindow::AddNumberToOperand(int i)
{
    QString strNumber;
    strNumber.setNum(i);
    QString strOperand = ui->textEdit->toPlainText();
    strOperand.append(strNumber);
    ui->textEdit->setText(strOperand);
}

void MainWindow::on_button0_clicked()
{
    AddNumberToOperand(0);
}

void MainWindow::on_button1_clicked()
{
    AddNumberToOperand(1);
}

void MainWindow::on_button2_clicked()
{
    AddNumberToOperand(2);
}

void MainWindow::on_button3_clicked()
{
    AddNumberToOperand(3);
}

void MainWindow::on_button4_clicked()
{
    AddNumberToOperand(4);
}

void MainWindow::on_button5_clicked()
{
    AddNumberToOperand(5);
}

void MainWindow::on_button6_clicked()
{
    AddNumberToOperand(6);
}

void MainWindow::on_button7_clicked()
{
    AddNumberToOperand(7);
}

void MainWindow::on_button8_clicked()
{
    AddNumberToOperand(8);
}

void MainWindow::on_button9_clicked()
{
    AddNumberToOperand(9);
}

